package com.icss.ah.categoryProduct.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.base.bean.SyncSelectTree;

@Service
@Transactional
public class CategoryProductService {
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	/**
	 * 获取商品分类树
	 */
	public List<SyncSelectTree> getTree(SyncSelectTree selectTree) {
		String id = selectTree.getId();//id 异步加载展开树时传到后台的id
		String ifAll = selectTree.getIfAll();//是否一次性展示所有数据，1是 0否
		List list = new ArrayList();
		try {
			if (ifAll != null && !"".equals(ifAll)) {
				if ("0".equals(ifAll)) {//展开树时加载数据
					if ("".equals(id)) {//如果查询id没有值，表示查询根节点
						list = sqlSessionTemplate.selectList("CategoryProductBean.getTree",selectTree);
					}else{//如果查询id与入口编码编码不相等，表示查询根节点下的子节点
						list = sqlSessionTemplate.selectList("CategoryProductBean.getTreeChild",selectTree);
					}
				}else{//一次加载所有数据
					if ("".equals(id)) {//如果查询id没有值，表示查询根节点
						list = sqlSessionTemplate.selectList("CategoryProductBean.getTreeAll",selectTree);
					}else{//如果查询id与入口编码编码不相等，表示查询根节点下的子节点
						list = sqlSessionTemplate.selectList("CategoryProductBean.getTreeChild",selectTree);
					}
				}
			}
			infolog.info("CategorySetService|getTree|list:"+list);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("CategorySetService|getTree", e);
		}
		return list == null ? new ArrayList() : list;
	}
	
	/**
	 * 查询商品参数列表
	 * @param map
	 * @return
	 */
	public List<Map> queryProductList(HashMap<String, String> map) {
		List<Map> list = new ArrayList<Map>();
		try{
			list = sqlSessionTemplate.selectList("CategoryProductBean.queryProductList", map);
			infolog.info("CategoryProductService|queryProductList|list:"+list);
		} catch (Exception ex) {
			ex.printStackTrace();
			errlog.error("CategoryProductService|queryProductList", ex);
		} 
		return list == null ? new ArrayList<Map>() : list;
	}

	/**
	 * 查询商品参数总记录数
	 * @param map
	 * @return
	 */
	public long queryProductCount(HashMap<String, String> map) {
		Long reValue = 0l;
		try {
			reValue = (Long)sqlSessionTemplate.selectOne("CategoryProductBean.queryProductCount", map);
			infolog.info("CategoryProductService|queryProductCount|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("CategoryProductService|queryProductCount", e);
		} 
		return reValue;
	}
	
	/**
	 * 查询参数信息
	 * @param map
	 * @return
	 */
	public HashMap queryParamMap(HashMap<String, Object> map) {
		HashMap reMap = new HashMap();
		try {
			reMap = (HashMap)sqlSessionTemplate.selectOne("CategoryProductBean.queryParamMap",map);
			infolog.info("CategoryProductService|queryParamMap|reMap:"+reMap);
		} catch (Exception ex) {
			ex.printStackTrace();
			errlog.error("CategoryProductService|queryParamMap", ex);
		} 
		return reMap = reMap == null ? new HashMap() : reMap;
	}
	
	
	
	
	
}
